using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Media3D;
using System.Windows;
using System.Windows.Controls;

namespace Sinacor.Infra.UI.Controls.SmartClient
{
    public class CardList : Selector
    {
        public CardList()
        {
            IsSynchronizedWithCurrentItem = false;
        }

        public double TiltAngle
        {
            get { return (double)GetValue(TiltAngleProperty); }
            set { SetValue(TiltAngleProperty, value); }
        }

        public double ItemGap
        {
            get { return (double)GetValue(ItemGapProperty); }
            set { SetValue(ItemGapProperty, value); }
        }

        public double FrontItemGap
        {
            get { return (double)GetValue(FrontItemGapProperty); }
            set { SetValue(FrontItemGapProperty, value); }
        }

        public double PopoutDistance
        {
            get { return (double)GetValue(PopoutDistanceProperty); }
            set { SetValue(PopoutDistanceProperty, value); }
        }

        public double ElementWidth
        {
            get { return (double)GetValue(ElementWidthProperty); }
            set { SetValue(ElementWidthProperty, value); }
        }

        public double ElementHeight
        {
            get { return (double)GetValue(ElementHeightProperty); }
            set { SetValue(ElementHeightProperty, value); }
        }

        public bool HasReflection
        {
            get { return (bool)GetValue(HasReflectionProperty); }
            set { SetValue(HasReflectionProperty, value); }
        }

        public ViewStateBase CurrentView
        {
            get { return (ViewStateBase)GetValue(CurrentViewProperty); }
            set { SetValue(CurrentViewProperty, value); }
        }

        public PerspectiveCamera Camera
        {
            get { return (PerspectiveCamera)GetValue(CameraProperty); }
            set { SetValue(CameraProperty, value); }
        }

        public static readonly DependencyProperty ElementHeightProperty = FlowPanel.ElementHeightProperty.AddOwner(typeof(CardList));
        public static readonly DependencyProperty ElementWidthProperty = FlowPanel.ElementWidthProperty.AddOwner(typeof(CardList));
        public static readonly DependencyProperty FrontItemGapProperty = FlowPanel.FrontItemGapProperty.AddOwner(typeof(CardList));
        public static readonly DependencyProperty HasReflectionProperty = FlowPanel.HasReflectionProperty.AddOwner(typeof(CardList));
        public static readonly DependencyProperty ItemGapProperty = FlowPanel.ItemGapProperty.AddOwner(typeof(CardList));
        public static readonly DependencyProperty PopoutDistanceProperty = FlowPanel.PopoutDistanceProperty.AddOwner(typeof(CardList));
        public static readonly DependencyProperty TiltAngleProperty = FlowPanel.TiltAngleProperty.AddOwner(typeof(CardList));
        public static readonly DependencyProperty CurrentViewProperty = FlowPanel.CurrentViewProperty.AddOwner(typeof(CardList));
        public static readonly DependencyProperty CameraProperty = FlowPanel.CameraProperty.AddOwner(typeof(CardList));

        protected override void OnMouseDoubleClick(System.Windows.Input.MouseButtonEventArgs e)
        {
            base.Items.MoveCurrentTo(base.SelectedItem);
            base.OnMouseDoubleClick(e);
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            return new CardListViewItem(this);
        }

        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            base.PrepareContainerForItemOverride(element, item);
        }

        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return item is CardListViewItem;
        }

        protected override void ClearContainerForItemOverride(DependencyObject element, object item)
        {
            base.ClearContainerForItemOverride(element, item);
        }

        protected override void OnItemsSourceChanged(System.Collections.IEnumerable oldValue, System.Collections.IEnumerable newValue)
        {
            this.Items.MoveCurrentTo(null);
            base.OnItemsSourceChanged(oldValue, newValue);
        }
    }

    public class CardListViewItem : ListBoxItem
    {
        public CardListViewItem(CardList cardListView)
        {
            _cardListView = cardListView;
            this.CardListView.Items.CurrentChanged += new EventHandler(Items_CurrentChanged);
        }

        private void Items_CurrentChanged(object sender, EventArgs e)
        {
            this.IsCurrent = (this.DataContext == this.CardListView.Items.CurrentItem);
        }

        private CardList _cardListView;
        protected CardList CardListView
        {
            get { return _cardListView; }
        }

        protected static readonly DependencyPropertyKey IsCurrentKey = DependencyProperty.RegisterReadOnly("IsCurrent", typeof(bool), typeof(CardListViewItem), new FrameworkPropertyMetadata(false));
        public static readonly DependencyProperty IsCurrentProperty = IsCurrentKey.DependencyProperty;

        public bool IsCurrent
        {
            get { return (bool)GetValue(IsCurrentProperty); }
            protected set { SetValue(IsCurrentKey, value); }
        }
    }
}